package org.flow.basic.thread.timer;

import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author xl-9527
 * @since 2024/8/11
 **/
public class TimerTestDemo implements ScheduledTestDemo<TimerTask> {

    private Timer timer;

    /**
     * 创建一个 Timer
     *
     * @param timerName Timer name
     * @return {@link  java.util.Timer}
     */
    private Timer getTimer(final String timerName) {
        if (Objects.isNull(timer)) {
            timer = Objects.isNull(timerName) ? new Timer() : new Timer(timerName);
        }
        return timer;
    }

    @Override
    public void newTask(final TimerTask task, final String timerName) {
        final Timer timer = this.getTimer(timerName);
        timer.schedule(task, 1000);
    }
}
